﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QLab.UserControls;
using QLab.Controllers;
using QLab.Utility;
using System.ComponentModel.Composition;
using System.Windows.Input;
using Microsoft.Practices.Prism.ViewModel;
using Microsoft.Practices.Prism.Commands;
using QLab.Controllers.SolutionExplorer;

namespace QLab.Controllers
{
	[Export]
	[PartCreationPolicy(CreationPolicy.Shared)]
	public class RibbonController : NotificationObject
	{
		private MainWindowController _mainWindowController;
		private SolutionExplorerController _solutionExplorerController;
		private TabController _tabController;

		private ICommand _createSolutionCommand;
		private ICommand _openSolutionCommand;
		private ICommand _closeApplicationCommand;

		[ImportingConstructor]
		public RibbonController(MainWindowController MWC, SolutionExplorerController SEC, TabController TC)
		{
			_mainWindowController = MWC;
			_solutionExplorerController = SEC;
			_tabController = TC;

			_createSolutionCommand = new DelegateCommand(CreateSolution);
			_openSolutionCommand = new DelegateCommand(OpenSolution);
			_closeApplicationCommand = new DelegateCommand(CloseApplication);

		}

		#region Properties

		public ICommand CreateSolutionCommand
		{
			get { return _createSolutionCommand; }
			set 
			{ 
				_createSolutionCommand = value;
				RaisePropertyChanged(() => CreateSolutionCommand);
			}
		}

		public ICommand OpenSolutionCommand
		{
			get { return _openSolutionCommand; }
			set
			{
				_openSolutionCommand = value;
				RaisePropertyChanged(() => OpenSolutionCommand);
			}
		}

		public ICommand CloseApplicationCommand
		{
			get { return _closeApplicationCommand; }
			set
			{
				_closeApplicationCommand = value;
				RaisePropertyChanged(() => CloseApplicationCommand);
			}
		}

		public TabController TabController { get { return _tabController; } }
		public SolutionExplorerController SolutionExplorerController { get { return _solutionExplorerController; } }

		#endregion

		#region Methods

		private void OpenSolution()
		{
			IO.Dialogs.ProjectOpenFileDialog dlg = new IO.Dialogs.ProjectOpenFileDialog();
			if (dlg.ShowDialog())
			{
				/*
				IO.Files.ProjectFileInfo pf = new IO.Files.ProjectFileInfo();
				if (pf.Load(dlg.FileName))
				{
				  MessageBox.Show(String.Format("Project file contains {0} folder(s).", pf.ProjectFolders.Count), "Project file ifo", MessageBoxButton.OK, MessageBoxImage.Information);
				}
				else
				{
				  MessageBox.Show(
				   String.Format("An error occurred while loading a file \"{0}\"!" , System.IO.Path.GetFileName(dlg.FileName)),
					"Info", MessageBoxButton.OK, MessageBoxImage.Exclamation);
				}
				*/
				// MainWindowController.Instance().SolutionExplorerController.LoadProject(pf);
				_solutionExplorerController.LoadSolution(dlg.FileName);
			}
		}

		private void CreateSolution()
		{
			frmNewProject fNP = new frmNewProject();
			fNP.ShowDialog();
			if (fNP.DialogResult.HasValue && fNP.DialogResult.Value)
			{
				_solutionExplorerController.CreateSolution(fNP.Location, fNP.SolutionName, fNP.ProjectName);
			}
		}

		private void CloseApplication()
		{
			_mainWindowController.CloseApplication();
		}

		#endregion
	}
}
